using System;
using WiMo.Games;
using WiMo.Games.Inputs;
using WiMo.Games.Drawables;
using System.Collections.Generic;

namespace Touch
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : IGame
    {
        private IInputManager _lastState;
        private ISpriteImage _moveableImage1;
        private ISpriteImage _moveableImage2;
        private ISpriteImage _moveableImage3;
        private ISpriteImage _moveableImage4;

        Dictionary<int, ISpriteImage> shapes = new Dictionary<int, ISpriteImage>();

        public Game(IGameEngine engine)
        {
            Engine = engine;
            Engine.ContentManager.ContentLocation = "Content";
            Engine.Display.BackgroundColor = Color.Yellow;
            Engine.EnableDiagnostics = true;
            Engine.DiagnosticsFontColor = Color.Black;
        }

        public void Initialize()
        {
            Engine.PhysicsManager.Gravity = new Vector2(2, 10);
            Engine.InputManager.RightSoftButton = "Exit";
            Engine.InputManager.MenuBarBackgroundColor = Color.Black;
            Engine.InputManager.SoftMenuFontColor = Color.White;            
        }

        public void BeginRun()
        {
            
        }

        public void CreateMenus()
        {
            
        }

        public void LoadContent()
        {
            _moveableImage1 = Engine.ContentManager.LoadImage("TouchMe1");
            _moveableImage1.Origin = Origin.Center;
            _moveableImage2 = Engine.ContentManager.LoadImage("TouchMe2");
            _moveableImage2.Origin = Origin.Center;
            _moveableImage3 = Engine.ContentManager.LoadImage("TouchMe3");
            _moveableImage3.Origin = Origin.Center;
            _moveableImage4 = Engine.ContentManager.LoadImage("TouchMe4");
            _moveableImage4.Origin = Origin.Center;

            _moveableImage1.IsVisible = false;
            _moveableImage2.IsVisible = false;
            _moveableImage3.IsVisible = false;
            _moveableImage4.IsVisible = false;
        }

        private ISpriteImage GetNextImage()
        {
            if (!_moveableImage1.IsVisible)
                return _moveableImage1;

            if (!_moveableImage2.IsVisible)
                return _moveableImage2;

            if (!_moveableImage3.IsVisible)
                return _moveableImage3;

            if (!_moveableImage4.IsVisible)
                return _moveableImage4;

            return null;
        }

        public void Update(WiMo.Games.GameTime gameTime)
        {
            if (Engine.InputManager.IsKeyDown(Keys.RightSoftButton))
                Engine.Exit();

            foreach(var touchPoint in Engine.InputManager.TouchPoints)
            {
                switch (touchPoint.State)
                {
                    case TouchPoint.TouchLocationState.Pressed:
                        var image = GetNextImage();
                        if (image != null)
                        {                            
                            shapes.Add(touchPoint.Id, image);
                            image.UnscaledPosition = new PointF(touchPoint.Position.X, touchPoint.Position.Y);
                            image.IsVisible = true;
                        }
                    break;
                    case TouchPoint.TouchLocationState.Moved:
                        if(shapes.ContainsKey(touchPoint.Id))
                            shapes[touchPoint.Id].UnscaledPosition = new PointF(touchPoint.Position.X, touchPoint.Position.Y);
                    break;
                    case TouchPoint.TouchLocationState.Released:
                    if (shapes.ContainsKey(touchPoint.Id))
                    {
                        shapes[touchPoint.Id].IsVisible = false;
                        shapes.Remove(touchPoint.Id);
                    }
                    break;
                }
            }

            _lastState = Engine.InputManager.GetKeysState();

            Engine.PhysicsManager.Update(gameTime);            
        }

        public void Draw(WiMo.Games.GameTime gameTime)
        {
            Engine.Display.Add(_moveableImage1);
            Engine.Display.Add(_moveableImage2);
            Engine.Display.Add(_moveableImage3);
            Engine.Display.Add(_moveableImage4);   
        }

        public bool CanExit()
        {
            return true;
        }

        public IGameEngine Engine {get; set;}
    }
}
